//
//  ResultTableViewCell.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/12.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "ResultTableViewCell.h"

@interface ResultTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *leftTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *playerNameLabel;


@end

@implementation ResultTableViewCell

- (void)setActionModel:(ActionModel *)actionModel{
    NSAssert(nil != _owner,@"请先给owner赋值");
    
    NSString *leftTitle,*score,*playerNames;
    
    BOOL transferFlag = NO;//是否是转雨收入
    BOOL isHost = NO;//当前cell是否为action的owner
    if ([actionModel.owner.playerId isEqualToString:_owner.playerId]) {
        isHost = YES;
    }
    
    if ([actionModel.actionType isEqualToString:@"杠"]) {
        if (actionModel.transferedTo) {//转雨 (杠上炮转雨) owner（杠的人）、targetArr（被杠的人）、transferTo（转雨给的人）
            if (isHost) {
                leftTitle = NSStringFormat(@"%@(已转雨)",actionModel.attributesArr.firstObject);
                score = @"0";
                playerNames = NSStringFormat(@"%@(转雨给%@)",[actionModel.targetPlayerNameArr componentsJoinedByString:@","],actionModel.transferedTo.playerName);
            }else{
                if ([actionModel.transferedTo.playerId isEqualToString:_owner.playerId]) {
                    transferFlag = YES;
                    leftTitle = @"转雨收入";
                    if ([actionModel.attributesArr.firstObject isEqualToString:@"直杠"]) {
                        if (actionModel.caguaArr.count) {//带擦挂
                            if ([actionModel.caguaPlayerIdArr containsObject:actionModel.transferedTo.playerId]) {
                                score = NSStringFormat(@"%ld",actionModel.score + (actionModel.caguaArr.count - 1)* actionModel.score/2);
                            }else if([actionModel.targetPlayerIdArr containsObject:actionModel.transferedTo.playerId]){
                                score = NSStringFormat(@"%ld",actionModel.caguaArr.count * actionModel.score/2);
                            }
                        }else{
                            score = @"0";
                        }
                    }else{
                        score = NSStringFormat(@"%ld",actionModel.score * (actionModel.targetsArr.count - 1));
                    }
                    playerNames = NSStringFormat(@"%@",actionModel.owner.playerName);
                }else{
                    leftTitle = NSStringFormat(@"被%@",actionModel.attributesArr.firstObject);
                    if ([actionModel.actionType isEqualToString:@"补杠"]) {
                        score = NSStringFormat(@"%ld",actionModel.score/2);
                    }else{
                        score = NSStringFormat(@"%ld",actionModel.score);
                    }
                    playerNames = NSStringFormat(@"%@(转雨给%@)",actionModel.owner.playerName,actionModel.transferedTo.playerName);
                }
            }
        }else if(actionModel.returned){//查叫后返雨
            if (isHost) {
                leftTitle = NSStringFormat(@"%@(已返雨)",actionModel.attributesArr.firstObject);
                score = @"0";
                playerNames = [actionModel.targetPlayerNameArr componentsJoinedByString:@","];
            }else{
                leftTitle = NSStringFormat(@"被%@(已返雨)",actionModel.attributesArr.firstObject);
                score = @"0";
                playerNames = actionModel.owner.playerName;
            }
        }else if ([actionModel.attributesArr containsObject:@"暗杠"] || [actionModel.attributesArr containsObject:@"补杠"]) {
            leftTitle = isHost?actionModel.attributesArr.firstObject:NSStringFormat(@"被%@",actionModel.attributesArr.firstObject);
            score = isHost?NSStringFormat(@"%ld",actionModel.score*actionModel.targetsArr.count):NSStringFormat(@"%ld",actionModel.score);
            playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
        }else{
            if (actionModel.caguaArr.count) {//带擦挂
                if (isHost) {
                    leftTitle = @"直杠";
                    score = NSStringFormat(@"%ld",actionModel.score + actionModel.caguaArr.count * actionModel.score/2);
                    playerNames = NSStringFormat(@"%@(擦挂:%@)",[actionModel.targetPlayerNameArr componentsJoinedByString:@","],[actionModel.caguaPlayerNameArr componentsJoinedByString:@","]);
                }else if ([actionModel.targetPlayerIdArr containsObject:_owner.playerId]) {//被直杠
                    leftTitle = @"点杠";
                    score = NSStringFormat(@"%ld",actionModel.score);
                    playerNames = actionModel.owner.playerName;
                }else {//被擦挂
                    leftTitle = @"被擦挂";
                    score = NSStringFormat(@"%ld",actionModel.score/2);
                    playerNames = actionModel.owner.playerName;
                }
            }else{
                leftTitle = isHost?@"直杠":@"点杠";
                score = NSStringFormat(@"%ld",actionModel.score);
                playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
            }
        }
    }else if ([actionModel.actionType isEqualToString:@"胡"]){
        if ([actionModel.attributesArr containsObject:@"杠上花"]) {
            leftTitle = isHost?@"杠上花":@"被杠上花";
            score = isHost?NSStringFormat(@"%ld",actionModel.score * actionModel.targetsArr.count):NSStringFormat(@"%ld",actionModel.score);
            playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
        }else if([actionModel.attributesArr containsObject:@"自摸"]){
            leftTitle = isHost?@"自摸":@"被自摸";
            score = isHost?NSStringFormat(@"%ld",actionModel.score * actionModel.targetsArr.count):NSStringFormat(@"%ld",actionModel.score);
            playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
        }else if([actionModel.attributesArr containsObject:@"杠上炮"]){
            leftTitle = isHost?@"杠上炮":@"被杠上炮";
            score = NSStringFormat(@"%ld",actionModel.score);
            playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
        }else{//平胡
            leftTitle = isHost?@"胡":@"点炮";
            score = NSStringFormat(@"%ld",actionModel.score);
            playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
        }
    }else if ([actionModel.actionType containsString:@"叫"]){
        leftTitle = isHost?@"查叫":@"被查叫";
        score = NSStringFormat(@"%ld",actionModel.score);
        playerNames = isHost?[actionModel.targetPlayerNameArr componentsJoinedByString:@","]:actionModel.owner.playerName;
    }
    
    //计算几个杠
    NSInteger idxHu = _cellIdx;
    for (NSInteger i = _cellIdx; i<_resultActionArr.count; i++) {
        ActionModel *am = _resultActionArr[i];
        if ([am.actionType isEqualToString:@"胡"] ) {
            idxHu = i;
            break;
        }
    }
    NSMutableArray *tmp = [NSMutableArray new];
    for (ActionModel *am in _resultActionArr) {
        NSInteger idx = [_resultActionArr indexOfObject:am];
        if ([am.actionType isEqualToString:@"杠"]
            && [am.owner.playerId isEqualToString:actionModel.owner.playerId]
            && idx < idxHu) {
            [tmp addObject:am];
        }
    }
    //胡牌属性
    NSMutableArray *attrArr = [NSMutableArray arrayWithArray:actionModel.attributesArr];
    [attrArr removeObjectsInArray:@[@"自摸",@"杠上花",@"杠上炮",@"直杠",@"暗杠",@"补杠"]];
    if (![actionModel.actionType isEqualToString:@"杠"] && tmp.count) {
        [attrArr addObject:NSStringFormat(@"%ld杠",tmp.count)];
    }
    NSString *tmpStr = @"";
    if (attrArr.count>0) {
        tmpStr = NSStringFormat(@" (%@)",[attrArr componentsJoinedByString:@","]);
    }
    
    //属性文字
    NSString *totalStr = NSStringFormat(@"%@%@",leftTitle,tmpStr);
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:totalStr];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:22] range:NSMakeRange(0, leftTitle.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, leftTitle.length)];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(leftTitle.length, totalStr.length - leftTitle.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(leftTitle.length, totalStr.length - leftTitle.length)];
    _leftTitleLabel.attributedText = attrStr;
    
    if (![score isEqualToString:@"0"]) {
        if (transferFlag) {
            _scoreLabel.textColor = [UIColor redColor];
            _scoreLabel.text = NSStringFormat(@"+%@",score);
        }else{
            _scoreLabel.textColor = isHost?[UIColor redColor]:UIColorHex(#8BB335);
            _scoreLabel.text = isHost?NSStringFormat(@"+%@",score):NSStringFormat(@"-%@",score);
        }
    }else{
        _scoreLabel.text = score;
        _scoreLabel.textColor = [UIColor grayColor];
    }
    _playerNameLabel.text = playerNames;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
